package com.example.casper.Experiment2024.Activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.casper.Experiment2024.Database.AppDatabase;
import com.example.casper.Experiment2024.Database.PunchRecord;
import com.example.casper.Experiment2024.R;

import java.io.File;

public class PunchDetailActivity extends AppCompatActivity {
    private TextView descriptionText;
    private ImageView photoView;
    private TextView locationText;
    private TextView timestampText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_punch_detail);

        descriptionText = findViewById(R.id.description_text);
        photoView = findViewById(R.id.photo_view);
        locationText = findViewById(R.id.location_text);
        timestampText = findViewById(R.id.timestamp_text);

        Intent intent = getIntent();
        long recordId = intent.getLongExtra("record_id", -1);

        if (recordId != -1) {
            PunchRecord record = AppDatabase.getInstance(this).punchRecordDao().getById((long) recordId);
            if (record != null) {
                descriptionText.setText(record.getDescription());
                if (record.getPhotoPath() != null && new File(record.getPhotoPath()).exists()) {
                    Bitmap bitmap = BitmapFactory.decodeFile(record.getPhotoPath());
                    photoView.setImageBitmap(bitmap);
                } else {
                    photoView.setImageResource(R.drawable.ic_launcher_background); // 设置默认图片或隐藏
                }
                locationText.setText("位置: " + record.getLatitude() + ", " + record.getLongitude());
                timestampText.setText("时间: " + record.getTimestamp());
            } else {
                Toast.makeText(this, "未找到对应打卡记录", Toast.LENGTH_SHORT).show();
                finish(); // 结束活动，避免显示空白页面
            }
        } else {
            Toast.makeText(this, "记录ID未正确传递", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}